﻿/*=========================================================================
 * AUTH: Matthew Baum, 9753095
 * DESC: Interface definition for a 2D Heads-Up Display.
 *=========================================================================*/

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace RisingThreat
{
    class HUD
    {
        // 2D Texture outlines for the HUD
        private Texture2D outline;

        // 2D Texture for HP bars, etc
        private Texture2D bar;

        // Font for HUD text
        private SpriteFont font;

        // Window dimensions
        private int windowHeight, windowWidth;

        // Time Span to count elasped time on the current level
        private TimeSpan levelTime = TimeSpan.Zero;

        // Time value used to calculate FPS rate
        private double refreshRate;

        //ammo left
        public int ammoCount=5;

        // Constructor
        public HUD(ContentManager content, int height, int width)
        {
            // Load necessary Content
            loadContent(content);

            // Set window dimensions
            windowHeight = height;
            windowWidth = width;
        }

        // Update the HUD
        public void update(TimeSpan elapsedTime)
        {
            // Update the time elasped on the current level
            levelTime += elapsedTime;

            // Set refresh time
            refreshRate = elapsedTime.TotalSeconds;
            refreshRate = 1.0 / refreshRate;
        }

        // Load graphics content
        public void loadContent(ContentManager content)
        {
            // Load outline Texture
            outline = content.Load<Texture2D>("hudOutline");

            // Load bar texture
            bar = content.Load<Texture2D>("bar");

            // Load font
            font = content.Load<SpriteFont>("hudFont");
        }

        // Draw the HUD
        public void draw(SpriteBatch sb, int playerLife)
        {
            // Draw the HUD outline
            drawOutline(sb);

            // Draw life bar
            drawLifeBar(sb, playerLife);

            // Draw HUD text
            drawText(sb);
        }

        // Draw the outline of the HUD
        private void drawOutline(SpriteBatch sb)
        {
            // Draw a 2px line
            sb.Draw(outline, new Vector2(10, windowHeight - outline.Height - 10), Color.White);
        }

        // Draw player life bar
        private void drawLifeBar(SpriteBatch sb, int playerLife)
        {
            if (playerLife <= 0)
            {
                playerLife = 0;
            }
            // Initial position
            Vector2 lifeBarPosition = new Vector2(22, windowHeight - 32);
            
            // Calculate damage taken
            byte life = (byte)Math.Ceiling( ( (double)playerLife / GameScreen.PLAYER_MAX_LIFE) * 255.0);
            byte damage = (byte)(255 - life);

            
                for (int i = 0; i != playerLife; ++i)
                {
                    // Draw a 2px line
                    sb.Draw(bar, lifeBarPosition, new Color( damage, life, 0) );

                    // Set position for next line
                    lifeBarPosition.X += 2;
                }
        }

        // Draw HUD text
        private void drawText(SpriteBatch sb)
        {
            // Draw some test strings
          
                // TEST
                //sb.DrawString(font, "FPS [ " + refreshRate + " ]", new Vector2(20, 20), Color.White);

                //sorry for putting this here and doing it like this, no time!
                ammoCount = 5;
                foreach(Bullet bullet in GameScreen.player.bullets)
                {
                    if (bullet.isAlive)
                        ammoCount--;
                }
                
                // Print ammo
                sb.DrawString(font, "AMMO "+ ammoCount, new Vector2(20, windowHeight - 50), Color.White);

                // Print level info
                sb.DrawString(font, "LEVEL " + Level.getLevelCounter(), new Vector2(350, windowHeight - 30), Color.White);

                // Print time
                sb.DrawString(font, levelTime.ToString("c").Substring(0, 8), new Vector2(550, windowHeight - 30), Color.White);
        }
    }
}
